package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.AppClassify;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.activity.app.BrandAreaActivity;
import com.linkage.educloud.ah.view.activity.app.EduAppListActivity;

import java.util.List;

/**
 * Created by Mr'Tang on 2018/12/17.
 */

public class AppClassifyAdapter extends android.widget.BaseAdapter {

    private Context mContext;
    private List<AppClassify> mList;

    public AppClassifyAdapter(Context context, List<AppClassify> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public AppClassify getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_app_classify, null);
            viewHolder = new ViewHolder();
            viewHolder.item_classify_icon = convertView.findViewById(R.id.item_classify_icon);
            viewHolder.item_classify_text = convertView.findViewById(R.id.item_classify_text);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        AppClassify info = getItem(position);
        ImageLoader.loadImg(mContext, info.getGroup_icon(), viewHolder.item_classify_icon);
        viewHolder.item_classify_text.setText(info.getGroupname());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (getItem(position).getGroupname()) {
                    case "品牌专区":
                        BrandAreaActivity.launch(mContext);
                        break;
                    case "我的应用":
                        EduAppListActivity.launch(mContext, getItem(position).getGroupname(), 3, 0,"");
                        break;
                    default:
                        EduAppListActivity.launch(mContext, getItem(position).getGroupname(), 1, getItem(position).getGroupid(),"");
                        break;
                }
            }
        });
        return convertView;
    }

    private class ViewHolder {
        ImageView item_classify_icon;
        TextView item_classify_text;
    }
}
